//
//  FFRecvSetAmountInputCell.m
//  LLWallet
//
//  Created by tt on 2020/12/18.
//

#import "FFRecvSetAmountInputCell.h"

@implementation FFRecvSetAmountInputModel

- (Class)cell {
    return FFRecvSetAmountInputCell.class;
}

@end

@interface FFRecvSetAmountInputCell ()<UITextFieldDelegate>

@property(nonatomic,strong) FFRecvSetAmountInputModel *model;

@property(nonatomic,strong) UIView *boxView;

@property(nonatomic,strong) UILabel *tipLbl;
@property(nonatomic,strong) UILabel *moneyLbl;
@property(nonatomic,strong) UITextField *inputFd;
@property(nonatomic,strong) UIButton *remarkBtn;
@property(nonatomic,strong) UIButton *nextBtn;

@property(nonatomic,copy) NSString *remark;

@end

@implementation FFRecvSetAmountInputCell

+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 240.0f);
}

- (void)setupUI {
    [super setupUI];
    [self setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:self.boxView];
    
    [self.boxView addSubview:self.tipLbl];
    [self.boxView addSubview:self.moneyLbl];
    [self.boxView addSubview:self.remarkBtn];
    [self.boxView addSubview:self.inputFd];
    [self.boxView addSubview:self.nextBtn];
}

- (void)refresh:(FFRecvSetAmountInputModel *)model {
    [super refresh:model];
    self.model = model;
    
    [self.remarkBtn setTitle:LLang(@"添加收钱备注") forState:UIControlStateNormal];
    [self.remarkBtn sizeToFit];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.boxView.lim_left = 10.0f;
    self.boxView.lim_width = self.contentView.lim_width - self.boxView.lim_left*2;
    self.boxView.lim_height = self.contentView.lim_height;
    
    CGFloat leftSpace = 20.0f;
    self.tipLbl.lim_left = leftSpace;
    self.tipLbl.lim_top = leftSpace;
    
    self.moneyLbl.lim_left = self.tipLbl.lim_left;
    self.moneyLbl.lim_top = self.tipLbl.lim_bottom + 10.0f;
    
    self.inputFd.lim_left = self.moneyLbl.lim_right + 10.0f;
    self.inputFd.lim_top = self.moneyLbl.lim_top;
    self.inputFd.lim_height = self.moneyLbl.lim_height;
    self.inputFd.lim_width = self.boxView.lim_width - 40.0f*2.0f;
    
    self.remarkBtn.lim_top = self.moneyLbl.lim_bottom + 30.0f;
    self.remarkBtn.lim_left = self.moneyLbl.lim_left;
    
    self.nextBtn.lim_left = 20.0f;
    self.nextBtn.lim_width = self.boxView.lim_width - self.nextBtn.lim_left*2;
    self.nextBtn.lim_height = 40.0f;
    self.nextBtn.lim_top = self.remarkBtn.lim_bottom + 30.0f;
    
    
}

- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        _boxView.backgroundColor = [WKApp shared].config.cellBackgroundColor;
        _boxView.layer.masksToBounds = YES;
        _boxView.layer.cornerRadius = 4.0f;
    }
    return _boxView;
}

- (UILabel *)tipLbl {
    if(!_tipLbl) {
        _tipLbl = [[UILabel alloc] init];
        _tipLbl.text = LLang(@"收款金额");
        _tipLbl.font = [[WKApp shared].config appFontOfSize:14.0f];
        [_tipLbl sizeToFit];
    }
    return _tipLbl;
}

- (UILabel *)moneyLbl {
    if(!_moneyLbl) {
        _moneyLbl = [[UILabel alloc] init];
        _moneyLbl.text = @"¥";
        _moneyLbl.font = [[WKApp shared].config appFontOfSizeSemibold:30.0f];
        [_moneyLbl sizeToFit];
    }
    return _moneyLbl;
}

- (UIButton *)remarkBtn {
    if(!_remarkBtn) {
        _remarkBtn = [[UIButton alloc] init];
        [_remarkBtn.titleLabel setFont:[[WKApp shared].config appFontOfSize:14.0f]];
        [_remarkBtn setTitleColor: [UIColor colorWithRed:96.0f/255.0f green:110.0f/255.0f blue:132.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
        [_remarkBtn addTarget:self action:@selector(remarkPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _remarkBtn;
}

-(void) remarkPressed {
    __weak typeof(self) weakSelf = self;
    UIAlertController *alertController =   [UIAlertController alertControllerWithTitle:LLang(@"添加收钱备注") message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.delegate = weakSelf;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:LLang(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:LLang(@"确认") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *remark = alertController.textFields[0].text;
        weakSelf.remark = remark;
        [weakSelf.remarkBtn setTitle:weakSelf.remark?:@"" forState:UIControlStateNormal];
        [weakSelf.remarkBtn sizeToFit];
        
    }]];
    [[WKNavigationManager shared].topViewController presentViewController:alertController animated:YES completion:nil];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSInteger length = [self textLength:newString];//转换过的长度
    if (length > 20){
        return NO;
    }

    return YES;
}
-(NSUInteger)textLength: (NSString *) text{

    NSUInteger asciiLength = 0;

      for (NSUInteger i = 0; i < text.length; i++) {
        unichar uc = [text characterAtIndex: i];
            //设置汉字和字母的比例  如何限制4个字符或12个字母 就是1:3  如果限制是6个汉字或12个字符 就是 1:2  一次类推
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger unicodeLength = asciiLength;
    return unicodeLength;

  }

- (UITextField *)inputFd {
    if(!_inputFd) {
        _inputFd = [[UITextField alloc] init];
        _inputFd.font = [[WKApp shared].config appFontOfSizeSemibold:30.0f];
        [_inputFd addTarget:self action:@selector(inputChange:) forControlEvents:UIControlEventEditingChanged];
        _inputFd.keyboardType = UIKeyboardTypeDecimalPad;
        [_inputFd becomeFirstResponder];
    }
    return _inputFd;
}

-(void) inputChange:(UITextField*)textFd {
    if(textFd.text.length>0) {
        self.nextBtn.enabled = YES;
        self.nextBtn.alpha = 1.0f;
    }else{
        self.nextBtn.enabled = NO;
        self.nextBtn.alpha = 0.4f;
    }
}

- (UIButton *)nextBtn {
    if(!_nextBtn) {
        _nextBtn = [[UIButton alloc] init];
        [_nextBtn setTitle:LLang(@"下一步") forState:UIControlStateNormal];
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.layer.cornerRadius = 4.0f;
        _nextBtn.backgroundColor = [WKApp shared].config.themeColor;
        _nextBtn.alpha = 0.4f;
        _nextBtn.enabled = NO;
        [_nextBtn addTarget:self action:@selector(nextPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

-(void) nextPressed {
    if(self.model.onNext) {
        self.model.onNext(self.inputFd.text, self.remark?:@"");
    }
}

@end
